<?php

namespace App\Http\Controllers\Admin;

use App\PrizeSetting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PrizeSettingController extends Controller
{
    public function index(Request $request){
        return view('admin.prizeSetting.list');
    }
    public function ajax_list(Request $request){

        $model= PrizeSetting::query()
        ;

        if($request->searchKey){
            $model=$model->where('name','like', '%'.$request->searchKey.'%');
        }
        $model=$model->paginate($request->limit)
            ->toArray();
        return $this->page_json_render($model);
    }
    //创建
    public function create(Request $request){
        return view('admin.prizeSetting.edit_add');
    }
    //储存
    public function store(Request $request){
        $name=$request->name;

        $this->filter($request);
        $model=new PrizeSetting();
        $model->name=$request->name;
        $model->chance=$request->chance;
        $model->image=$request->image;
        $model->save();
        return redirect()->route('admin.prizeSetting_index')->with(
            $this->success()
        );
    }
    //编辑
    public function edit(Request $request,$id){
        $model=PrizeSetting::findOrFail($id);
        return view('admin.prizeSetting.edit_add',compact('id','model'));
    }
    public function update(Request $request){
        $id=$request->id;
        $this->filter($request);
        $model=PrizeSetting::findOrFail($id);
        $model->name=$request->name;
        $model->chance=$request->chance;
        $model->image=$request->image;
        $model->save();
        return redirect()->route('admin.prizeSetting_index')->with(
            $this->success()
        );
    }
    public function del(Request $request){
        $id=$request->id;
        $model=PrizeSetting::findOrFail($id);
        $model->delete();
        return $this->del_success();
    }
    protected function filter($request){
        $this->validate($request,
            [
                'name'=>'required',
                'chance'=>'required'
            ]);
    }
}
